<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          v-hasPermi="['purchase:pfmMstr:query']"
          @click="handleSearch"
        >
          搜索
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['purchase:pfmMstr:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['purchase:pfmMstr:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="handleDelete"
          v-hasPermi="['purchase:pfmMstr:del']"
        >
          删除
        </el-button>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-check"
            size="mini"
            @click="handleSubmit"
            :disabled="single"
            v-hasPermi="['purchase:pfmMstr:submit']"
          >
            提交
          </el-button>
        </el-col>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['purchase:pfmMstr:export']"
        >
          导出
        </el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      height="calc(100% - 300px)"
      v-loading="loading"
      :data="pfmMstrList"
      @row-click="clickRow"
      :cell-style="iCellStyle"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" width="40" align="center" />
      <el-table-column
        label="采购单号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmDjbh"
      />
      <el-table-column
        label="厂商"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmCsdm"
      >
        <template #default="scope">
          <dict-tag :options="allPfmCsdmOptions" :value="scope.row.pfmCsdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="采购预期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pfmJhrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pfmJhrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmDesc"
      />
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmNgdesc"
      />
      <el-table-column
        label="单据状态"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmZtmc"
      />
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pfmJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pfmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pfmShrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pfmShrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleChildrenAdd(scope.row)"
            v-hasPermi="['purchase:pfmMstr:addChildren']"
            :disabled="scope.row.pfmTjflag != 0 || scope.row.pfmZtbz != 1"
          >
            添加
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--采购明细-->
    <el-tabs v-model="activeTab">
      <el-tab-pane label="采购明细" name="first">
        <el-table
          border
          stripe
          height="220px"
          :data="pfdDetList"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column label="请购单号" align="center" prop="pfdQfdh" />
          <el-table-column label="物料代码" align="center" prop="pfdWldm" />
          <el-table-column label="品名规格" align="center" prop="pfdPmgg" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="pfdDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.pfdDw" />
            </template>
          </el-table-column>
          <el-table-column label="采购数量" align="center" prop="pfdCgsl" />
          <el-table-column label="采购单价" align="center" prop="pfdCgdj" />
          <el-table-column label="采购金额" align="center" prop="pfdCgje" />
          <el-table-column label="备注" align="center" prop="pfdDesc" />
          <el-table-column label="变更原因" align="center" prop="pfdBgdesc" />
          <el-table-column label="申请部门" align="center" prop="pfdBmdm" />
          <el-table-column label="申请人员" align="center" prop="pfdQgry" />
          <el-table-column label="建立人员" align="center" prop="pfdJlry" />
          <el-table-column label="建立日期" align="center" prop="pfdJlrq" />
          <el-table-column label="变更人员" align="center" prop="pfdBgry" />
          <el-table-column label="变更日期" align="center" prop="pfdBgrq" />
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="100"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleChildrenDel(scope.row)"
                :disabled="single"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="采购统计" name="second">
        <el-table border stripe height="220px" :data="pfdDetTotal">
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column label="物料代码" align="center" prop="pfdWldm" />
          <el-table-column label="品名规格" align="center" prop="pfdPmgg" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="pfdDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.pfdDw" />
            </template>
          </el-table-column>
          <el-table-column label="采购数量" align="center" prop="pfdCgsl" />
          <el-table-column label="采购单价" align="center" prop="pfdCgdj" />
          <el-table-column label="采购金额" align="center" prop="pfdCgje" />
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="建立日期">
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="采购单号" prop="pfmDjbh">
            <el-input
              v-model="queryParams.pfmDjbh"
              placeholder="请输入采购单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="厂商" prop="pfmCsdm">
            <el-select
              v-model="queryParams.pfmCsdm"
              placeholder="请选择厂商"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in pfmCsdmOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="状态标志" prop="pfmZtbz">
            <el-radio-group v-model="queryParams.pfmZtbz">
              <el-radio
                v-for="dict in ZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改零星采购单对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="680px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="24">
          <el-form-item label="采购单号" prop="pfmDjbh">
            <el-input
              v-model="form.pfmDjbh"
              placeholder="流水单号生成"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="厂商" prop="pfmCsdm">
            <el-select v-model="form.pfmCsdm" placeholder="请选择厂商" disabled>
              <el-option
                v-for="dict in pfmCsdmOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采购预期" prop="pfmJhrq">
            <el-date-picker
              clearable
              size="small"
              v-model="form.pfmJhrq"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择采购预期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="pfmDesc">
            <el-input
              type="textarea"
              v-model="form.pfmDesc"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      v-model="addOpen"
      customClass="customWidth"
      :fullscreen="true"
      :close-on-click-modal="false"
      append-to-body
    >
      <!-- 添加物料搜索条件框-->
      <el-form :model="queryChildParams" :inline="true">
        <el-form-item label="请购单号" prop="pfdQfdh">
          <el-input
            v-model="queryChildParams.pfdQfdh"
            placeholder="请输入请购单号"
            clearable
            size="small"
            @keyup.enter.native="searchChildren"
          />
        </el-form-item>
        <el-form-item label="品名规格" prop="pfdPmgg">
          <el-input
            v-model="queryChildParams.pfdPmgg"
            placeholder="请输入品名规格"
            clearable
            size="small"
            @keyup.enter.native="searchChildren"
          />
        </el-form-item>
        <el-form-item style="justify-content: flex-end">
          <el-button type="primary" @click="searchChildren">查询</el-button>
        </el-form-item>
      </el-form>
      <!--待接收物料-->
      <el-form>
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="searchList"
          @selection-change="handleSelectionChangeC"
          border
          stripe
          ref="checkTable"
          :height="tableH"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column
            type="selection"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="请购单号"
            :show-overflow-tooltip="true"
            align="center"
            prop="pfdQfdh"
          />
          <el-table-column
            label="物料品名"
            :show-overflow-tooltip="true"
            align="center"
            prop="itm_wlpm"
          />
          <el-table-column
            label="物料规格"
            :show-overflow-tooltip="true"
            align="center"
            prop="itm_wlgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="pfdDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itm_dw" />
            </template>
          </el-table-column>
          <el-table-column
            label="请购数量"
            :show-overflow-tooltip="true"
            align="center"
            prop="lqm_qgsl"
          />
          <el-table-column
            label="用途厂商"
            :show-overflow-tooltip="true"
            align="center"
            prop="pfdDesc"
          />
          <el-table-column
            label="参考单价"
            :show-overflow-tooltip="true"
            align="center"
            prop="itm_price"
          />
          <el-table-column
            label="采购数量"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
          >
            <template #default="scope">
              <el-input
                class="inputName"
                type="number"
                v-model="scope.row.pfdCgsl"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column
            label="采购单价"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
          >
            <template #default="scope">
              <el-input
                class="inputName"
                type="number"
                v-model="scope.row.pfdCgdj"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
          >
            <template #default="scope">
              <el-input
                class="inputName"
                v-model="scope.row.pfdDesc"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            align="center"
            prop="pfdWldm"
          />
          <el-table-column
            label="需求日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="lqm_xqrq"
          />
          <el-table-column
            label="申请部门"
            :show-overflow-tooltip="true"
            align="center"
            prop="lqm_bmdm"
          />
          <el-table-column
            label="申请人员"
            :show-overflow-tooltip="true"
            align="center"
            prop="lqm_jlry"
          />
          <el-table-column
            label="申请日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="lqm_jlrq"
          />
        </el-table>

        <!--采购明细-->
        <el-tabs v-model="activeTab">
          <el-tab-pane label="采购明细" name="first">
            <el-table
              border
              stripe
              :height="tableHChild"
              :data="pfdDetList"
              :header-cell-style="{ 'text-align': 'center' }"
            >
              <el-table-column
                label="#"
                type="index"
                align="center"
                fixed="left"
              />
              <el-table-column label="请购单号" align="center" prop="pfdQfdh" />
              <el-table-column label="物料代码" align="center" prop="pfdWldm" />
              <el-table-column label="品名规格" align="center" prop="pfdPmgg" />
              <el-table-column
                label="单位"
                :show-overflow-tooltip="true"
                align="center"
                prop="pfdDw"
              >
                <template #default="scope">
                  <dict-tag
                    :options="unitFlagOptions"
                    :value="scope.row.pfdDw"
                  />
                </template>
              </el-table-column>
              <el-table-column label="采购数量" align="center" prop="pfdCgsl" />
              <el-table-column label="采购单价" align="center" prop="pfdCgdj" />
              <el-table-column label="采购金额" align="center" prop="pfdCgje" />
              <el-table-column label="备注" align="center" prop="pfdDesc" />
              <el-table-column
                label="变更原因"
                align="center"
                prop="pfdBgdesc"
              />
              <el-table-column label="申请部门" align="center" prop="pfdBmdm" />
              <el-table-column label="申请人员" align="center" prop="pfdQgry" />
              <el-table-column label="建立人员" align="center" prop="pfdJlry" />
              <el-table-column label="建立日期" align="center" prop="pfdJlrq" />
              <el-table-column label="变更人员" align="center" prop="pfdBgry" />
              <el-table-column label="变更日期" align="center" prop="pfdBgrq" />
              <el-table-column
                label="操作"
                align="center"
                fixed="right"
                width="100"
                class-name="small-padding fixed-width"
              >
                <template #default="scope">
                  <el-button
                    size="mini"
                    type="text"
                    @click="handleChildrenDel(scope.row)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="采购统计" name="second">
            <el-table border stripe :height="tableHChild" :data="pfdDetTotal">
              <el-table-column
                label="#"
                type="index"
                align="center"
                fixed="left"
              />
              <el-table-column label="物料代码" align="center" prop="pfdWldm" />
              <el-table-column label="品名规格" align="center" prop="pfdPmgg" />
              <el-table-column
                label="单位"
                :show-overflow-tooltip="true"
                align="center"
                prop="pfdDw"
              >
                <template #default="scope">
                  <dict-tag
                    :options="unitFlagOptions"
                    :value="scope.row.pfdDw"
                  />
                </template>
              </el-table-column>
              <el-table-column label="采购数量" align="center" prop="pfdCgsl" />
              <el-table-column label="采购单价" align="center" prop="pfdCgdj" />
              <el-table-column label="采购金额" align="center" prop="pfdCgje" />
            </el-table>
          </el-tab-pane>
        </el-tabs>
        <!-- 底部确定取消 -->
        <el-form-item
          style="margin-top: 25px; display: flex; justify-content: flex-end"
        >
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="handAddChildren">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listpfmMstr,
  listPfDdet,
  getpfmMstr,
  delpfmMstr,
  addpfmMstr,
  updatepfmMstr,
  searchData,
  saveDate,
  pfdDetDel,
  submitPfM,
} from "@/api/purchase/pfmMstr";
import { getAllCs, getCs } from "@/api/putStorage/putStorageAdd";

export default {
  name: "pfmMstr",
  data() {
    return {
      //表格高度
      tableH: (document.body.clientHeight - 130) / 2,
      tableHChild: (document.body.clientHeight - 280) / 2,
      tableHAdd: document.body.clientHeight - 320,
      activeTab: "first",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      pfmId: null,
      //选中数据
      clickData: {},
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 零星采购单表格数据
      pfmMstrList: [],
      // 零星采购单明细表格数据
      pfdDetList: [],
      pfdDetTotal: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      addOpen: false,
      //状态标志字典
      ZtbzOptions: [],
      // 厂商字典
      pfmCsdmOptions: [],
      // 全部厂商
      allPfmCsdmOptions: [],
      //单位
      unitFlagOptions: [],
      // 建立日期时间范围
      dateRange: [
        new Date(new Date().getTime() - 3600 * 1000 * 24 * 30),
        new Date(new Date().getTime() + 3600 * 1000 * 24 * 60),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        pfmDjbh: null,
        pfmCsdm: null,
        pfmZtbz: "1",
        pfmJlrq: null,
      },
      searchList: [],
      totalChildren: 0,
      selectedList: [],
      queryChildParams: {
        pageNum: 1,
        pageSize: 25,
        pfdDjbh: null,
        pfdQfdh: null,
        pfdPmgg: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pfmCsdm: [
          { required: true, message: "厂商不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_purchase_state").then((response) => {
      this.ZtbzOptions = response.data;
    });
    getCs().then((response) => {
      this.pfmCsdmOptions = response.data;
      for (let item of this.pfmCsdmOptions) {
        item.dictLabel = item.csmCs;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });

    getAllCs().then((response) => {
      this.allPfmCsdmOptions = response.data;
      for (let item of this.allPfmCsdmOptions) {
        item.dictLabel = item.csmCsmc;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });

    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
  },
  methods: {
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //未审核
      if (row.pfmZtbz == "1" && row.pfmTjflag == 1) {
        return "padding:0px 0;color:green";
      }
      //已审核
      else if (row.pfmZtbz == "2" && row.pfmTjflag == 1) {
        return "padding:0px 0;color:#bb0331";
      }
    },
    /** 查询零星采购单列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.params["beginPfmJlrq"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endPfmJlrq"] = this.moment(
          this.dateRange[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listpfmMstr(this.queryParams).then((response) => {
        this.pfmMstrList = [];
        this.pfdDetList = [];
        this.pfdDetTotal = [];
        this.pfmMstrList = this.pfmMstrList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickRow(row);
        } else if (this.pfmMstrList != null && this.pfmMstrList.length > 0) {
          this.clickRow(this.pfmMstrList[0]);
        }
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 25;
        this.queryParams.pageNum++;
        listpfmMstr(this.queryParams).then((response) => {
          this.pfmMstrList = this.pfmMstrList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.addOpen = false;
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pfmId: null,
        pfmDjbh: null,
        pfmCsdm: null,
        pfmJhrq: new Date(),
        pfmZtbz: null,
        pfmTjflag: null,
        pfmDesc: null,
        pfmNgdesc: null,
        pfmJlry: null,
        pfmJlrq: null,
        pfmShry: null,
        pfmShrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.pfmMstrList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //选中单据
    clickRow(row) {
      this.clickData = row;
      this.single = !(
        row != null &&
        row.pfmZtbz == "1" &&
        row.pfmTjflag == "0"
      );
      this.pfmId = row.pfmId;
      this.getPfdList(row.pfmDjbh);
    },
    getPfdList(pfmDjbh) {
      listPfDdet(pfmDjbh).then((res) => {
        this.pfdDetList = res.data.detail;
        this.pfdDetTotal = res.data.total;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.pfmCsdm = "HD000";
      this.open = true;
      this.title = "添加零星采购单";
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.reset();
      const pfmId = this.pfmId;
      getpfmMstr(pfmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改零星采购单";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询零星采购单";
      this.openSearch = true;
    },
    /** 添加 */
    handleChildrenAdd(row) {
      this.addOpen = true;
    },
    /** 查询需要添加的数据 */
    searchChildren() {
      this.queryChildParams.pfdDjbh = this.clickData.pfmDjbh;
      this.queryChildParams.pageNum = 1;
      searchData(this.queryChildParams).then((response) => {
        this.searchList = response.rows;
        this.totalChildren = response.total;
      });
    },
    /** 添加数据多选项 */
    handleSelectionChangeC(selection) {
      this.selectedList = selection;
    },
    /** 添加 - 保存数据 */
    handAddChildren() {
      const data = this.selectedList;
      if (data.length > 0) {
        if (this.isEmpty(data)) {
          this.msgError("采购数量不能为0");
          return;
        } else {
          saveDate(data)
            .then((response) => {
              if (response.code == "200") {
                this.searchChildren();
                this.getPfdList(this.clickData.pfmDjbh);
                this.msgSuccess("添加成功");
              }
            })
            .catch(() => {
              this.msgError("添加失败");
            });
        }
      } else {
        alert("请选择需要添加的数据");
      }
    },
    /** 添加-保存-效验数据是否为空 */
    isEmpty(data) {
      for (let i of data) {
        if (i.pfdCgsl == null || i.pfdCgsl <= 0) return true;
      }
      return false;
    },
    /** 子项数据 删除 */
    handleChildrenDel(row) {
      pfdDetDel(row.pfdId, row.pfdQfdh).then((res) => {
        if (res.code == 200) {
          this.getPfdList();
          this.msgSuccess("删除成功");
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.pfmId != null) {
            updatepfmMstr(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addpfmMstr(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pfmId = this.pfmId;
      this.$confirm(
        '是否确认删除零星采购单编号为"' + this.clickData.pfmDjbh + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delpfmMstr(pfmId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 提交按钮操作 */
    handleSubmit() {
      if (this.pfdDetList.length <= 0) {
        this.msgError(
          "订单:【" + this.clickData.pfmDjbh + "】,无明细物料,无法提交"
        );
        return;
      }

      const pfmId = this.clickData.pfmId;
      submitPfM(pfmId)
        .then((res) => {
          this.getList();
          this.msgSuccess("提交成功");
        })
        .catch(() => {
          this.msgError("提交失败");
        });
    },
    /** 导出按钮操作 */
    handleExport() {},
  },
};
</script>

<style lang="scss" scoped></style>
